<template>
  <div class="login-container">
    <div class="login-form">
      <!-- Logo区域 -->
      <div class="logo-section">
        <img src="@/assets/logo.png" alt="Logo" class="logo" />
        <h2 class="login-title">AI智能作业批改系统</h2>
        <p class="login-subtitle">智能高效 · 精准批改 · 提升教学</p>
      </div>
      
      <a-form
        :model="formState"
        :rules="rules"
        ref="formRef"
        @finish="handleSubmit"
        class="login-form-content"
      >
        <a-form-item name="username">
          <a-input
            v-model:value="formState.username"
            placeholder="请输入用户名"
            size="large"
            class="input-field"
          >
            <template #prefix>
              <user-outlined class="input-icon" />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item name="password">
          <a-input-password
            v-model:value="formState.password"
            placeholder="请输入密码"
            size="large"
            class="input-field"
          >
            <template #prefix>
              <lock-outlined class="input-icon" />
            </template>
          </a-input-password>
        </a-form-item>
        
        <a-form-item class="form-options">
          <a-checkbox v-model:checked="rememberMe" class="remember-checkbox">
            记住我
          </a-checkbox>
          <a class="forgot-password" href="/forgot-password">忘记密码?</a>
        </a-form-item>
        
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            class="login-button"
            size="large"
            :loading="loading"
            block
          >
            <span v-if="!loading">立即登录</span>
            <span v-else>登录中...</span>
          </a-button>
        </a-form-item>
        
        <div class="register-section">
          <span>还没有账号？</span>
          <a href="/register" class="register-link">立即注册</a>
        </div>
      </a-form>
    </div>
    
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import type { FormInstance } from 'ant-design-vue'
import { useUserStore } from '@/store/user'
import { setStorage, getStorage } from '@/utils/storage'

interface FormState {
  username: string
  password: string
}

// 表单引用
const formRef = ref<FormInstance>()
// 表单数据
const formState = reactive<FormState>({
  username: '',
  password: '',
})
// 记住我选项
const rememberMe = ref(false)
// 登录状态
const loading = ref(false)

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 表单验证规则
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

// 初始化数据
const initFormData = () => {
  // 获取存储的用户名和记住我选项
  const savedUsername = getStorage<string>('savedUsername')
  const savedRememberMe = getStorage<boolean>('rememberMe', false)
  
  if (savedUsername && savedRememberMe) {
    formState.username = savedUsername
    rememberMe.value = true
  }
}

// 提交表单
const handleSubmit = async () => {
  loading.value = true
  try {
    // 保存用户名和记住我选项
    if (rememberMe.value) {
      setStorage('savedUsername', formState.username)
      setStorage('rememberMe', true)
    } else {
      setStorage('savedUsername', '')
      setStorage('rememberMe', false)
    }
    
    // 使用表单中的username和password字段进行登录
    await userStore.login(formState.username, formState.password)
    
    message.success('登录成功！欢迎使用AI智能作业批改系统')
    
    // 获取重定向的URL
    const redirect = route.query.redirect as string
    router.push(redirect || '/')
  } catch (error) {
    message.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}

// 初始化
initFormData()
</script>

<style lang="less" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('@/assets/bg.png');
  background-size: cover;
  background-position: center;  position: relative;
  overflow: hidden;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  
  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(52, 152, 219, 0.1);
    animation: float 6s ease-in-out infinite;
    
    &.circle-1 {
      width: 200px;
      height: 200px;
      top: 10%;
      left: 10%;
      animation-delay: 0s;
      background: rgba(52, 152, 219, 0.08);
    }
    
    &.circle-2 {
      width: 150px;
      height: 150px;
      top: 70%;
      right: 15%;
      animation-delay: 2s;
      background: rgba(41, 128, 185, 0.06);
    }
    
    &.circle-3 {
      width: 100px;
      height: 100px;
      bottom: 20%;
      left: 20%;
      animation-delay: 4s;
      background: rgba(31, 97, 141, 0.05);
    }
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.login-form {
  width: 420px;
  background: transparent;
  border-radius: 20px;
  box-shadow: none;
  backdrop-filter: none;
  border: 2px solid rgba(255, 255, 255, 0.3);
  overflow: hidden;
  position: relative;
  z-index: 10;
}

.logo-section {
  text-align: center;
  padding: 40px 30px 30px;
  background: transparent;
  color: #2c3e50;
  position: relative;
  border-bottom: 1px solid rgba(255, 255, 255, 0.4);
  
  .logo {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    margin-bottom: 20px;
    border: 3px solid rgba(44, 62, 80, 0.2);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
    object-fit: cover;
    background: rgba(255, 255, 255, 0.9);
  }
  
  .login-title {
    font-size: 24px;
    font-weight: 700;
    margin: 0 0 8px 0;
    color: #2c3e50;
    text-shadow: 0 1px 3px rgba(255, 255, 255, 0.8);
  }
  
  .login-subtitle {
    font-size: 14px;
    margin: 0;
    color: #34495e;
    font-weight: 400;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8);
  }
}

.login-form-content {
  padding: 30px 40px 40px;
  background: rgba(255, 255, 255, 0.296);
  border-radius: 0;
  backdrop-filter: blur(10px);
  border: none;
  box-shadow: none;
  
  .input-field {
    border-radius: 12px;
    border: 2px solid #e0e6ed;
    transition: all 0.3s ease;
    background: rgba(255, 255, 255, 0.878);
    
    &:hover {
      border-color: #bdc3c7;
    }
    
    &:focus-within {
      border-color: #3498db;
      box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
    }
    
    :deep(.ant-input) {
      border: none;
      padding: 12px 16px;
      font-size: 16px;
      background: transparent;
      
      &:focus {
        box-shadow: none;
      }
      
      &::placeholder {
        color: #95a5a6;
      }
    }
    
    :deep(.ant-input-password) {
      border: none;
      background: transparent;
      
      .ant-input {
        border: none;
        padding: 12px 16px;
        font-size: 16px;
        background: transparent;
        
        &::placeholder {
          color: #95a5a6;
        }
      }
    }
  }
  
  .input-icon {
    color: #3498db;
    font-size: 18px;
  }
  
  .form-options {
    margin-bottom: 24px;
    
    :deep(.ant-form-item-control-input) {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .remember-checkbox {
      color: #2c3e50;
      
      :deep(.ant-checkbox-checked .ant-checkbox-inner) {
        background-color: #3498db;
        border-color: #3498db;
      }
      
      :deep(.ant-checkbox-inner) {
        border-color: #bdc3c7;
      }
      
      :deep(.ant-checkbox:hover .ant-checkbox-inner) {
        border-color: #3498db;
      }
    }
    
    .forgot-password {
      color: #3498db;
      font-size: 14px;
      text-decoration: none;
      transition: color 0.3s ease;
      
      &:hover {
        color: #2980b9;
        text-decoration: underline;
      }
    }
  }
  
  .login-button {
    height: 50px;
    border-radius: 12px;
    background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
    border: none;
    font-size: 16px;
    font-weight: 600;
    box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
    transition: all 0.3s ease;
    color: white;
    
    &:hover:not(:disabled) {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
      background: linear-gradient(135deg, #2980b9 0%, #1f618d 100%);
    }
    
    &:active {
      transform: translateY(0);
    }
    
    &:disabled {
      background: #bdc3c7;
      color: #7f8c8d;
      box-shadow: none;
    }
  }
  
  .register-section {
    text-align: center;
    margin-top: 20px;
    font-size: 14px;
    color: #2c3e50;
    
    .register-link {
      color: #3498db;
      text-decoration: none;
      font-weight: 600;
      margin-left: 8px;
      transition: color 0.3s ease;
      
      &:hover {
        color: #2980b9;
        text-decoration: underline;
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-form {
    width: 90%;
    max-width: 380px;
    margin: 20px;
  }
  
  .logo-section {
    padding: 30px 20px 20px;
    
    .logo {
      width: 60px;
      height: 60px;
    }
    
    .login-title {
      font-size: 20px;
    }
    
    .login-subtitle {
      font-size: 12px;
    }
  }
  
  .login-form-content {
    padding: 20px 30px 30px;
    
    .input-field {
      :deep(.ant-input) {
        padding: 10px 14px;
        font-size: 14px;
      }
    }
    
    .login-button {
      height: 45px;
      font-size: 14px;
    }
  }
  
  .bg-decoration {
    .circle {
      &.circle-1 {
        width: 120px;
        height: 120px;
      }
      
      &.circle-2 {
        width: 100px;
        height: 100px;
      }
      
      &.circle-3 {
        width: 80px;
        height: 80px;
      }
    }
  }
}

@media (max-width: 480px) {
  .login-form {
    width: 95%;
    border-radius: 15px;
  }
  
  .logo-section {
    padding: 25px 15px 15px;
    
    .login-title {
      font-size: 18px;
    }
  }
  
  .login-form-content {
    padding: 15px 20px 25px;
  }
}
</style> 